<?php
    ob_start();
    session_start();

    $page_title = "Promeni novina | Box";
    include "../includes/header_snimki.php";
    include '../includes/common.php';

//PROVERKA DALI IMA VALIDNA SESIQ
    if(!isset($_SESSION['isLogged']) || $_SESSION['isLogged'] !== true) {
        session_destroy();
        header('Location:index.php');
        exit;
    }

    $q_check_get = "SELECT MAX(new_id) as mx FROM news";
    $result1 = mysqli_query($db_connect, $q_check_get);
    $author_count = mysqli_fetch_array($result1, MYSQLI_ASSOC);
    $mx = $author_count['mx'];

    if(array_key_exists('new', $_GET) && (int)$_GET['new'] <= $mx && (int)$_GET['new'] > 0) {
        $new_id = $_GET['new'];

        if(isset($_POST['edit'])) {
            $edited_title = trim(htmlspecialchars($_POST['new_title']));
            $edited_text = trim(htmlspecialchars($_POST['new_text']));

            $q1 = "UPDATE news SET new_title = '$edited_title', new_text = '$edited_text'
            WHERE new_id ='$new_id'";

            $result1 = mysqli_query($db_connect, $q1);
        }

        $q = "SELECT new_id, new_title, new_text, new_pic_loc FROM news WHERE new_id = '$new_id'";

        $result = mysqli_query($db_connect, $q);

        $selected_new = array();

        while($row = mysqli_fetch_assoc($result)) {
            $selected_new['new_id'] = $row['new_id'];
            $selected_new['new_title'] = $row['new_title'];
            $selected_new['new_text'] = $row['new_text'];
            $selected_new['new_pic_loc'] = $row['new_pic_loc'];
        }


?>

<?php if(isset($selected_new['new_id']) && isset($_POST['edit']) && $result1 === true) : ?>
    <br/><br/><br/><p>Успешно редактирахте записа</p>
    <a href="index.php"><h3>Върни се обратно.</h3></a>
<?php endif; ?>

<?php if (isset($selected_new['new_id']) && !isset($_POST['edit']) ) : ?>
    <br/><br/><br/><br/><a href="index.php"><h3>Върни се обратно.</h3></a><hr><br/>
    <div>
        <h3>Система за промяна на данните за съществуващи новини: </h3><br/>
        <form method="POST" action="" style="width: 500px; display:inline-block;">
            Заглавие на новината:<input type="text" name="new_title" size="60"
                                    value="<?php echo $selected_new['new_title'];?>"/><br/>
            Текст на новината:<br/><textarea rows="12" cols="50"
                                    name="new_text"><?php echo $selected_new['new_text'];?></textarea><br/>
            <input type="submit" name="edit" value="Запиши промените"/>
        </form>
        <img style="display:inline-block;" src="../<?php echo $selected_new['new_pic_loc'];?>"/>
    </div>
    <br/><br/><br/>
<?php endif; ?>

<?php

    if (!isset($selected_new['new_id'])) {
        echo 'Грешен идентификационен номер на новина.<a href="index.php"><h3>Върни се обратно.</h3></a>';
    }
    require "../includes/footer_snimki.php";

    } else {
    echo 'Грешен идентификационен номер на новина.<a href="index.php"><h3>Върни се обратно.</h3></a>';
    require "../includes/footer_snimki.php";
    }
?>